package com.liu.project.aspect;

import com.liu.project.kmeans.ClusterModel;
import com.liu.project.kmeans.kmeans;
import com.liu.project.service.CertificateService;
import com.liu.project.utils.RedisUtil;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.*;
import java.util.HashSet;

/**
 * @program: project
 * @description: 推荐算法执行织入
 * @author: 86187
 * @create: 2022-03-20 09:32
 **/

@Aspect
@Component
public class RocmAspect {


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    CertificateService service;

    private static Logger LOGGER = LoggerFactory.getLogger(RocmAspect.class);



    @Pointcut(value = "@annotation(com.liu.project.aspect.RocmAspect.OperLog)")
    public void rocm(){}

    @AfterReturning("rocm()")
    public void doAfterReturning() {
        kmeans kmeans = new kmeans(redisUtil,service);
        ClusterModel model = kmeans.train(120);

        //当添加证书后，将聚类缓存删除
        HashSet<String> xy= (HashSet<String>) redisUtil.keys("xy:*");
        for (String string : xy) {
            redisUtil.del(string);
        }

        HashSet<String> y= (HashSet<String>) redisUtil.keys("y:*");
        for (String string : y) {
            redisUtil.del(string);
        }
        LOGGER.info("AfterReturning执行=====》");
    }

    @Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
    @Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
    @Documented //生成文档
    public @interface OperLog {
        String value() default "";
    }
}
